package com.security2.demo.authentication;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.security2.demo.domain.ReturnInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @ClassName AuthLimitHandler
 * @Description权限不足处理器
 * @Author Liujt
 * @Date 2020/1/15 17:02
 **/
@Component
public class AuthLimitHandler implements AccessDeniedHandler {

    @Autowired
    private ObjectMapper objectMapper;

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response,
                       AccessDeniedException accessDeniedException) throws IOException {
        response.setStatus(HttpStatus.OK.value());
        response.getWriter().write(objectMapper.writeValueAsString(new ReturnInfo<>("权限不足")));
    }
}

